package com.sam.algorithm;

import org.apache.commons.codec.digest.Md5Crypt;
import org.junit.jupiter.api.Test;
import org.apache.commons.codec.digest.DigestUtils;

/**
 * @Author: Sam
 * @Date: 2022-08-11 15:18
 * @Description: MD5加密
 */
public class MD5Encrypt {

    @Test
    public void MD5(){
        //md5加密
        String s1 = DigestUtils.md5Hex("123456");
        String s2 = DigestUtils.md5Hex("123456");
        String s3 = DigestUtils.md5Hex("123456 ");
        String s4 = DigestUtils.md5Hex(" 123456");
        System.out.println("s1 = " + s1);
        System.out.println("s2 = " + s2);
        System.out.println("s3 = " + s3);
        System.out.println("s4 = " + s4);
    }

    @Test
    public void MD5Crypt(){
        //md5随机盐值加密
        String s1 = Md5Crypt.md5Crypt("123456".getBytes());
        String s2 = Md5Crypt.md5Crypt("123456".getBytes());
        String s3 = Md5Crypt.md5Crypt("123456 ".getBytes());
        String s4 = Md5Crypt.md5Crypt(" 123456".getBytes());
        System.out.println("s1 = " + s1);
        System.out.println("s2 = " + s2);
        System.out.println("s3 = " + s3);
        System.out.println("s4 = " + s4);
        //s1 = $1$Q9qGB48B$q7ho5XuSLQzdAn0gJgsXm0
        //s2 = $1$68r5p9dw$H2131gCbAECTUcnWVOXk11
        //s3 = $1$.Wx9A61D$WzieeAm.htbVlbe1mO8Ux1
        //s4 = $1$lTCH2wIJ$UWP7cS3jQfLqtAVwcGi3i.
    }

    @Test
    public void MD5DIYCrypt(){
        //md5自定义盐值加密 需要以$1$作为前缀
        String s1 = Md5Crypt.md5Crypt("123456".getBytes(),"$1$888888");
        String s2 = Md5Crypt.md5Crypt("123456".getBytes(),"$1$888888");
        String s3 = Md5Crypt.md5Crypt("123456 ".getBytes(),"$1$888888");
        String s4 = Md5Crypt.md5Crypt(" 123456".getBytes(),"$1$888888");
        System.out.println("s1 = " + s1);
        System.out.println("s2 = " + s2);
        System.out.println("s3 = " + s3);
        System.out.println("s4 = " + s4);
        //s1 = $1$888888$ZVLgs3Si8w4INwF4vhJXW.
        //s2 = $1$888888$ZVLgs3Si8w4INwF4vhJXW.
        //s3 = $1$888888$U1o9OH1/Jm.qXmS3ZRYx91
        //s4 = $1$888888$9hAFHq5hyr2doQxEW0EWn1
        //md5自定义盐值加密 需要以$apr1$作为前缀 可不加自动填充
        String s5 = Md5Crypt.apr1Crypt("123456","$apr1$888888");
        String s6 = Md5Crypt.apr1Crypt("123456","888888");
        String s7 = Md5Crypt.apr1Crypt("123456 ","$apr1$888888");
        String s8 = Md5Crypt.apr1Crypt(" 123456","$apr1$888888");
        System.out.println("s5 = " + s5);
        System.out.println("s6 = " + s6);
        System.out.println("s7 = " + s7);
        System.out.println("s8 = " + s8);
        //s5 = $apr1$888888$/jnxSnmwQOBgnoKW8K2a3.
        //s6 = $apr1$888888$/jnxSnmwQOBgnoKW8K2a3.
        //s7 = $apr1$888888$kqVzeJmHP5tgHUXZmduNX0
        //s8 = $apr1$888888$DyD3f71CIFk92WfQIaZas0
    }
}
